package com.lanjiu.lib.imclient.dto;

import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.ims.IMSContacts;

/**
 * @description: 群组
 * @author: Panwei
 * @date: 2019/11/14 16:50
 */
public class GroupDto extends Dto {

    // 当前用户类型
    private String currentUserType;

    // 群组ID
    private String groupId;

    // 群组名称
    private String groupName;

    // 群头像
    private String groupAvatar;

    // 群人数
    private int memberCount;

    // 群类型(游客用户群，注册用户群，混合群)
    private String groupType;

    // 群主用户ID
    private String groupOwnerUserId;

    // 群公告ID
    private String announcementId;

    //群公告标题
    private String announcementTitle;

    //群公告内容
    private String announcementContent;

    // 阅后即焚
    private boolean snapchatStatus;

    // 阅后即焚时长
    private int snapchatTimeLength;

    // 阅后即焚时间单位
    private int snapchatTimeUnit;

    public GroupDto(String currentUserType) {
        this.currentUserType = currentUserType;
    }

    public String getCurrentUserType() {
        return currentUserType;
    }

    public void setCurrentUserType(String currentUserType) {
        this.currentUserType = currentUserType;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupAvatar() {
        return groupAvatar;
    }

    public void setGroupAvatar(String groupAvatar) {
        this.groupAvatar = groupAvatar;
    }

    public int getMemberCount() {
        return memberCount;
    }

    public void setMemberCount(int memberCount) {
        this.memberCount = memberCount;
    }

    public String getGroupType() {
        return groupType;
    }

    public void setGroupType(String groupType) {
        this.groupType = groupType;
    }

    public String getGroupOwnerUserId() {
        return groupOwnerUserId;
    }

    public void setGroupOwnerUserId(String groupOwnerUserId) {
        this.groupOwnerUserId = groupOwnerUserId;
    }

    public String getAnnouncementId() {
        return announcementId;
    }

    public void setAnnouncementId(String announcementId) {
        this.announcementId = announcementId;
    }

    public String getAnnouncementTitle() {
        return announcementTitle;
    }

    public void setAnnouncementTitle(String announcementTitle) {
        this.announcementTitle = announcementTitle;
    }

    public String getAnnouncementContent() {
        return announcementContent;
    }

    public void setAnnouncementContent(String announcementContent) {
        this.announcementContent = announcementContent;
    }

    public boolean isSnapchatStatus() {
        return snapchatStatus;
    }

    public void setSnapchatStatus(boolean snapchatStatus) {
        this.snapchatStatus = snapchatStatus;
    }

    public int getSnapchatTimeLength() {
        return snapchatTimeLength;
    }

    public void setSnapchatTimeLength(int snapchatTimeLength) {
        this.snapchatTimeLength = snapchatTimeLength;
    }

    public int getSnapchatTimeUnit() {
        return snapchatTimeUnit;
    }

    public void setSnapchatTimeUnit(int snapchatTimeUnit) {
        this.snapchatTimeUnit = snapchatTimeUnit;
    }

    public GroupDto(BusinessProtocolEntities.TouristsGroup group) {
        this.currentUserType = IMSContacts.UserType.TOURISTS_USER;
        this.groupId = group.getGroupId();
        this.groupName = group.getGroupName();
        this.groupAvatar = group.getGroupAvatar();
        this.memberCount = group.getMemberCount();
        this.groupType = group.getGroupType();
        this.groupOwnerUserId = group.getGroupOwnerUserId();
        BusinessProtocolEntities.GroupAnnouncements groupAnnouncements = group.getGroupAnnouncements();
        if (null != groupAnnouncements) {
            this.announcementId = groupAnnouncements.getAnnouncementId();
            this.announcementTitle = groupAnnouncements.getAnnouncementTitle();
            this.announcementContent = groupAnnouncements.getAnnouncementContent();
        }
        BusinessProtocolEntities.Settings settings = group.getSettings();
        if (null != settings) {
            this.snapchatStatus = settings.getSnapchatStatus();
            this.snapchatTimeLength = settings.getSnapchatTimeLength();
            this.snapchatTimeUnit = settings.getSnapchatTimeUnit();
        }
    }

    public GroupDto(BusinessProtocolEntities.RegisteredGroup group) {
        this.currentUserType = IMSContacts.UserType.REGISTERED_USER;
        this.groupId = group.getGroupId();
        this.groupName = group.getGroupName();
        this.groupAvatar = group.getGroupAvatar();
        this.memberCount = group.getMemberCount();
        this.groupType = group.getGroupType();
        this.groupOwnerUserId = group.getGroupOwnerUserId();
        BusinessProtocolEntities.GroupAnnouncements groupAnnouncements = group.getGroupAnnouncements();
        if (null != groupAnnouncements) {
            this.announcementId = groupAnnouncements.getAnnouncementId();
            this.announcementTitle = groupAnnouncements.getAnnouncementTitle();
            this.announcementContent = groupAnnouncements.getAnnouncementContent();
        }
        BusinessProtocolEntities.Settings settings = group.getSettings();
        if (null != settings) {
            this.snapchatStatus = settings.getSnapchatStatus();
            this.snapchatTimeLength = settings.getSnapchatTimeLength();
            this.snapchatTimeUnit = settings.getSnapchatTimeUnit();
        }
    }

    @Override
    public Object toProtobuf() {
        if (IMSContacts.UserType.REGISTERED_USER.equals(currentUserType)) {
            BusinessProtocolEntities.RegisteredGroup.Builder builder = BusinessProtocolEntities.RegisteredGroup.newBuilder();
            if (null != groupId) builder.setGroupId(groupId);
            if (null != groupName) builder.setGroupName(groupName);
            if (null != groupAvatar) builder.setGroupAvatar(groupAvatar);
            builder.setMemberCount(memberCount);
            if (null != groupType) builder.setGroupType(groupType);
            if (null != groupOwnerUserId) builder.setGroupOwnerUserId(groupOwnerUserId);

            if (null != announcementId || null != announcementTitle || null != announcementContent) {
                BusinessProtocolEntities.GroupAnnouncements.Builder builderAnnouncements = BusinessProtocolEntities.GroupAnnouncements.newBuilder();
                if (null != announcementId) builderAnnouncements.setAnnouncementId(announcementId);
                if (null != announcementTitle)
                    builderAnnouncements.setAnnouncementTitle(announcementTitle);
                if (null != announcementContent)
                    builderAnnouncements.setAnnouncementContent(announcementContent);
                builder.setGroupAnnouncements(builderAnnouncements.build());
            }

            return builder.setSettings(BusinessProtocolEntities.Settings.newBuilder()
                    .setSnapchatStatus(snapchatStatus)
                    .setSnapchatTimeLength(snapchatTimeLength)
                    .setSnapchatTimeUnit(snapchatTimeUnit)
                    .build()).build();
        } else {
            BusinessProtocolEntities.TouristsGroup.Builder builder = BusinessProtocolEntities.TouristsGroup.newBuilder();
            if (null != groupId) builder.setGroupId(groupId);
            if (null != groupName) builder.setGroupName(groupName);
            if (null != groupAvatar) builder.setGroupAvatar(groupAvatar);
            builder.setMemberCount(memberCount);
            if (null != groupType) builder.setGroupType(groupType);
            if (null != groupOwnerUserId) builder.setGroupOwnerUserId(groupOwnerUserId);
            if (null != announcementId || null != announcementTitle || null != announcementContent) {
                BusinessProtocolEntities.GroupAnnouncements.Builder builderAnnouncements = BusinessProtocolEntities.GroupAnnouncements.newBuilder();
                if (null != announcementId) builderAnnouncements.setAnnouncementId(announcementId);
                if (null != announcementTitle)
                    builderAnnouncements.setAnnouncementTitle(announcementTitle);
                if (null != announcementContent)
                    builderAnnouncements.setAnnouncementContent(announcementContent);
                builder.setGroupAnnouncements(builderAnnouncements.build());
            }

            return builder.setSettings(BusinessProtocolEntities.Settings.newBuilder()
                    .setSnapchatStatus(snapchatStatus)
                    .setSnapchatTimeLength(snapchatTimeLength)
                    .setSnapchatTimeUnit(snapchatTimeUnit)
                    .build()).build();
        }
    }
}
